package com.f1ashback.bus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.f1ashback.bus.Qo.BusQo;
import com.f1ashback.bus.Vo.BusGet;
import com.f1ashback.bus.Vo.BusShowInfo;
import com.f1ashback.bus.Vo.BusVo;
import com.f1ashback.bus.common.AjaxResult;
import com.f1ashback.bus.entity.Bus;
import com.f1ashback.bus.mapper.BusMapper;
import com.f1ashback.bus.mapper.StationMapper;
import com.f1ashback.bus.service.BusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class BusServiceImpl extends ServiceImpl<BusMapper, Bus> implements BusService {

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private BusMapper busMapper;

    @Override
    public List<BusVo> listBus() {
        log.warn("获取公交列表");
        List<Bus> buses = busMapper.listBus();
        List<BusVo> busVos = resVoList(buses);
        return busVos;
    }

    @Override
    public AjaxResult add(BusGet bus) {
        Bus ins = new Bus();
        BeanUtils.copyProperties(bus, ins);
        ins.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //获取经停站点ID
        try {
            String buss = "";
            List<String> stopSta = bus.getStopSta();
            for (String s : stopSta) {
                buss += s + ",";
            }
            buss = buss.substring(0, buss.length() - 1);
            ins.setStopSta(buss);

            //设置公交ID
            String te = bus.getBusNo() + "f1ash6ack";
            byte[] bytes = te.getBytes();
            int a = 0;
            for (byte aByte : bytes) {
                a += aByte;
            }
            Long r = Long.valueOf(a) + Long.valueOf(String.valueOf(System.currentTimeMillis()).substring(7));
            ins.setBusId(r);
            ins.setStartSta(Integer.valueOf(bus.getStartSta()));
            ins.setEndSta(Integer.valueOf(bus.getEndSta()));
            busMapper.insert(ins);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("添加失败！");
        }
        return AjaxResult.success("添加成功！");
    }

    @Override
    public AjaxResult update(BusGet bus) {
        Bus upd = new Bus();
        BeanUtils.copyProperties(bus, upd);
        if (bus.getStopSta().size() > 0) {
            String buss = "";
            List<String> stopSta = bus.getStopSta();
            for (String s : stopSta) {
                buss += s + ",";
            }
            buss = buss.substring(0, buss.length() - 1);
            upd.setStopSta(buss);
        }
        if (bus.getStartSta() != null && bus.getEndSta() != null) {
            upd.setStartSta(Integer.valueOf(bus.getStartSta()));
            upd.setEndSta(Integer.valueOf(bus.getEndSta()));
        }
        int i = busMapper.updateById(upd);
        if (i <= 0) {
            return AjaxResult.error("修改失败！");
        }
        return AjaxResult.success("修改成功！");
    }

    @Override
    public Integer delete(Long busId) {
        log.warn(busId.toString());
        int i = busMapper.deleteById(busId);
        return i;
    }

    @Override
    public List<String> listBusNo(Long busId) {
        List<String> busNo = busMapper.listBusNo(busId);
        return busNo;
    }

    @Override
    public List<BusVo> applistBus() {
        log.info("获取Bus数据");
        List<Bus> list = busMapper.listBus();
        return resVoList(list);
    }

    @Override
    public List<BusVo> getBusInfoByBusNo(String busNo) {
        log.info("获取Bus数据");
        List<Bus> busList = busMapper.selectBusByBusNo(busNo);
        return resVoList(busList);
    }

    @Override
    public AjaxResult getBusIdAndNo() {
        Map<String, List<BusShowInfo>> res = new HashMap<>();
        List<BusShowInfo> list = busMapper.getBusShowInfo();
        for (BusShowInfo busShowInfo : list) {
            if (busShowInfo.getStartSta() != null && busShowInfo.getEndSta() != null) {
                busShowInfo.setStartSta(stationMapper.getStationById(Integer.valueOf(busShowInfo.getStartSta())));
                busShowInfo.setEndSta(stationMapper.getStationById(Integer.valueOf(busShowInfo.getEndSta())));
            }
        }
        res.put("res", list);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult queryBus(BusQo busQo) {
        Map<String, List<BusVo>> res = new HashMap<>();
        Bus queryBusI = new Bus();
        BeanUtils.copyProperties(busQo, queryBusI);
        Integer startStaId = null;
        Integer endStaId = null;
        if (busQo.getStartSta() != null && busQo.getStartSta() != "") {
            startStaId = Integer.valueOf(busQo.getStartSta());

        }
        if (busQo.getEndSta() != null && busQo.getEndSta() != ""){
            endStaId = Integer.valueOf(busQo.getEndSta());
        }
        queryBusI.setStartSta(startStaId);
        queryBusI.setEndSta(endStaId);
        if (busQo.getStatus() != null && busQo.getStatus() != "" && busQo.getStatus().equals("正常")) {
            queryBusI.setStatus(1);
        } else if (busQo.getStatus() != null && busQo.getStatus() != "" && busQo.getStatus().equals("未启用")) {
            queryBusI.setStatus(0);
        } else {
            queryBusI.setStatus(null);
        }
        List<Bus> busList = busMapper.queryBus(queryBusI);
        res.put("res", resVoList(busList));
        return AjaxResult.success(res);
    }

    private List<BusVo> resVoList(List<Bus> list) {
        List<BusVo> busVoList = new ArrayList<>();
        if (list.isEmpty()) {
            return null;
        }
        for (Bus bus : list) {
            if (bus != null) {
                BusVo busVo = new BusVo();
                BeanUtils.copyProperties(bus, busVo);
                if (bus.getStatus() != null && bus.getStatus() == 1) {
                    busVo.setStatus("正常");
                } else {
                    busVo.setStatus("未启用");
                }
                String res = "";
                String stopSta = bus.getStopSta();
                String[] split = stopSta.split(",");
                for (String s : split) {
                    String station = stationMapper.getStationById(Integer.valueOf(s));
                    res += station + "→";
                }
                res = res.substring(0, res.length() - 1);
                busVo.setStopSta(res);
                if (bus.getStartSta() != null && bus.getEndSta() != null) {
                    String startSta = stationMapper.getStationById(bus.getStartSta());
                    String endSta = stationMapper.getStationById(bus.getEndSta());
                    busVo.setStartSta(startSta);
                    busVo.setEndSta(endSta);
                }
                busVoList.add(busVo);
            }
        }
        return busVoList;
    }
}
